package SFRZ;

import org.apache.commons.codec.binary.Base64;

import java.security.KeyPair;
import java.security.PrivateKey;

import static SFRZ.RSA.getPrivateKey;

/**
 * @author konley
 * @date 2021-12-11 11:26
 */
public class SenderA {
    //签名明文m
    private String m = "i am a";
    private String puk;
    private String sign;
    private String prk;
    //获取A的公钥
    public String getPuk() {
        return puk;
    }
    //获取A的明文
    public String getM(){
        return m;
    }
    //签名并发送
    public String send() throws Exception {
        //生成密钥对
        KeyPair keyPair = RSA.getKeyPair();
        prk = new String(Base64.encodeBase64(keyPair.getPrivate().getEncoded()));
        puk = new String(Base64.encodeBase64(keyPair.getPublic().getEncoded()));
        System.out.println("私钥:" + puk);
        System.out.println("公钥:" + prk);
        // RSA签名
        sign = RSA.sign(m, RSA.getPrivateKey(prk));
        return sign;
    }

}
